Jump to content

Add "\P" infront of each item of a list


Grrr

Recommended Posts

Hi again guys,

Today I've came up with an idea for "creating" mtext, before inserting it in the drawing.

I use similar lisp for a single-line mtext (just type and pick insertion point), since I hate the standard command with defining the mtext box first and then type.

 

However this one differs as it allows the user to type every row, and when its done to insert it. A list is constructed, where each item represents a new row, but I'm having issues with the "\P" addition infront of each item, and I can't make the "multiline" mtext:

; Attempt to type MTEXT:
; Construct list of strings, when done add "\P" between items, then entmake mtext with content of that list
; In user-speaking language: type rows for the mtext until done, then insert the mtext

(defun c:test ( / ans mtxt-list pt1)

(setq cont T)
(setq mtxt-list (list))
(initget "Finish")

; how to loop to type mtexts and pick points? (loop the loop)
(while cont ; loop to type rows
	(setq ans (getstring t "\nType a row for mtext or [Finish] : ")) ; ask for content of the row
	(cond
		( (or (and (= ans "Finish") mtxt-list ) (and (= ans "FINISH") mtxt-list ) (and (= ans "F") mtxt-list ) (and (= ans "f") mtxt-list ) ) ; check if the list is not nil
			(setq cont F) ; stop loop for typing rows
			(setq pt1 (getpoint "\nSpecify insertion point for the mtext:"))
			(M-Text pt1 (substr (vl-princ-to-string (reverse mtxt-list)) 2 (- (strlen (vl-princ-to-string mtxt-list)) 2)) ) ; removed first and last syntax
		)
		(t 
			(setq mtxt-list (cons (strcat (substr "x\P" 2) ans) mtxt-list)) ; how to add "\P" infornt each item?
			(print (reverse mtxt-list))(princ (strcat ", " (itoa (length mtxt-list)) " rows."))
		); t
	);cond
);while

(princ)
);defun	

; LM
(defun M-Text (pt str)
(entmakex 
	(list 
		(cons 0 "MTEXT")         
		(cons 100 "AcDbEntity")
		(cons 100 "AcDbMText")
		(cons 10 pt)
		(cons 1 str)
		(cons 71 5)
	)
)
)

The next (not so big) problem that I have is that I'm trying to while loop this entire thing, so after the mtext is entmake'd the user would be prompt for typing a row for a new mtext. (and so on.. type rows for mtext, pick point.. and so on).

 

I've commented parts of the code for you to understand easier what I'm doing there. Thanks in advance!

Link to comment
Share on other sites

Some thoughts to chew.

 

(defun c:test (/ go lst ans p)
 (setq go t lst "")
 (while (and go (setq ans (getstring t "\nType a row for mtext or [Enter to Finish] :")))
   (cond ((and (/= ans "") (eq (type ans) 'str))
          (setq lst (strcat lst ans "\\P"))
          )
         ((and (eq ans "")
               (/= lst "")
               (setq p (getpoint "\nSpecify insertion point for the mtext:"))
               )
          (entmakex (list '(0 . "MTEXT")
                          '(100 . "AcDbEntity")
                          '(100 . "AcDbMText")
                    (cons 10 (trans p 1 0))
                    (cons 1 lst)
                    '(71 . 5)
                    )) 
          (setq go nil))
         ((= ans "") (setq go nil))
         )
   )
 (princ)
 )

Link to comment
Share on other sites

I did a quick try and seems to work, later I'll analyse it more carefully and I'll try to figure out how to loop this loop.

At first glance I see that you skipped my substr and vl-princ-to-string functions, so new information would be provided for me.

Thank you again, Tharwat! :)

Link to comment
Share on other sites

Just did it, looped the whole thing:

 

(defun c:test (/ go2 go lst ans p)

(setq go2 T)

(while go2
	(setq go T)
	(setq lst "")
	(while (and go (setq ans (getstring t "\nType a row for mtext or [Enter to Finish] :")))
		(cond 
			( (and (/= ans "") (eq (type ans) 'str))
				(setq lst (strcat lst ans "\\P"))
			)
			
			( (and (eq ans "") (/= lst "") (setq p (getpoint "\nSpecify insertion point for the mtext:")))
				(if (and
					(entmakex 
						(list 
							'(0 . "MTEXT")
							'(100 . "AcDbEntity")
							'(100 . "AcDbMText")
							(cons 10 (trans p 1 0))
							(cons 1 lst)
							'(71 . 5)
						)
					) 
					(setq go nil)
				)
				(setq go2 T)
				)
			)
			( (= ans "")
				(setq go nil)
				(setq go2 F)
			)
		); cond
	); sub-while
); main while

(princ)
);defun

There might be some illogical conditions, but everything works fine.

I'm impressed how you figured to use a fake list, and just construct a very long string while separating the inputs with "\\P" ! :D

Link to comment
Share on other sites

Thats an interesting thread, BIGAL

I usually don't format my text/mtext objects, but I'll see if I can learn something new from these codes.

The solutions (codes) posted in there seem to become more and more complex, and there are always these additional modification requests (for personal usage).

Link to comment
Share on other sites

 (and (entmakex (list  '(0 . "MTEXT") '(100 . "AcDbEntity")'(100 . "AcDbMText")(cons 10 (trans p 1 0))(cons 1 lst)'(71 . 5))) 
         	(setq go nil)
          )

The above and function will NOT return true since one of the expressions is equal to nil.

 

(setq go2 F)			

 

I think it is better to set a variable to nil than set it to a null symbol as in your case.

Link to comment
Share on other sites

Did corrections on the code, upon Tharwats comment:

(defun c:test (/ go2 go lst ans p)

(setq go2 T)

(while go2
	(setq go T)
	(setq lst "")
	(while (and go (setq ans (getstring t "\nType a row for mtext or [Enter to Finish] :")))
		(cond 
			( (and (/= ans "") (eq (type ans) 'str))
				(setq lst (strcat lst ans "\\P"))
			)
			
			( (and (eq ans "") (/= lst "") (setq p (getpoint "\nSpecify insertion point for the mtext:")))
				(if 
					(entmakex 
						(list 
							'(0 . "MTEXT")
							'(100 . "AcDbEntity")
							'(100 . "AcDbMText")
							(cons 10 (trans p 1 0))
							(cons 1 lst)
							'(71 . 5)
						)
					) 
					(progn
						(setq go nil)
						(setq go2 T)
					)
				)
			)
			( (= ans "")
				(setq go nil)
				(setq go2 nil)
			)
		); cond
	); sub-while
); main while

(princ)
);defun

Link to comment
Share on other sites

A bit less coding if you are interested. :)

(setq go nil
     go2 t)

 

Nice found!

I'm not used with this way of writing the code (writing the setq function just like progn), not sure whys that - maybe its easier to trace (if the code has problem) by myself. :)

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...